import requests
from requests.cookies import RequestsCookieJar
import re
from selenium import webdriver
import time

from my_base import MyBase
from datetime import datetime
from information import USER,PASSWORD

class YinYI(MyBase):
    headers = {
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Content-Type': 'text/html; charset=utf-8',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
    }
    headers1 = {
        'Host': 'ke.yijincc.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
        'Cookie': 'QCWx_0e98_saltkey=oqxj2K7A; QCWx_0e98_lastvisit=1595566806; QCWx_0e98_sid=IgM2M1; QCWx_0e98_lastact=1595573103%09plugin.php%09; QCWx_0e98_ulastactivity=7349aB%2BnHWJvm%2F0OY8Ogn2%2BIzORN7ehDUS50RYLaXMI%2BTWSCId49; QCWx_0e98_auth=c3aejRfM%2Bu2fBQudy1Opu%2FpQEsr2UShe6PMMhhsNZtY8nTKSabvu7oDgYWktwow3xVt0ipH5mug1N%2FRpq%2FreWEOM; QCWx_0e98_lastcheckfeed=3652%7C1595573103; QCWx_0e98_checkfollow=1; QCWx_0e98_lip=110.19.61.151%2C1595573057'
    }
    cookies = [
        'QCWx_0e98_sid=IgM2M1; expires=Sat, 25-Jul-2020 06:45:03 GMT; Max-Age=86400; path=/; domain=.yijincc.com; secure',
        'QCWx_0e98_lastact=1595573103%09plugin.php%09; expires=Sat, 25-Jul-2020 06:45:03 GMT; Max-Age=86400; path=/; domain=.yijincc.com; secure',
        'QCWx_0e98_ulastactivity=d1061QDHVsSH%2BdLQtdN2HG9JGuSDM1Pxb0jhqolrJ4pCG7s5klaz; expires=Sat, 24-Jul-2021 06:45:03 GMT; Max-Age=31536000; path=/; domain=.yijincc.com; secure',
        'QCWx_0e98_sid=IgM2M1; expires=Sat, 25-Jul-2020 06:45:03 GMT; Max-Age=86400; path=/; domain=.yijincc.com; secure',
        'QCWx_0e98_ulastactivity=7349aB%2BnHWJvm%2F0OY8Ogn2%2BIzORN7ehDUS50RYLaXMI%2BTWSCId49; expires=Sat, 24-Jul-2021 06:45:03 GMT; Max-Age=31536000; path=/; domain=.yijincc.com; secure',
        'QCWx_0e98_auth=c3aejRfM%2Bu2fBQudy1Opu%2FpQEsr2UShe6PMMhhsNZtY8nTKSabvu7oDgYWktwow3xVt0ipH5mug1N%2FRpq%2FreWEOM; expires=Sun, 23-Aug-2020 06:45:03 GMT; Max-Age=2592000; path=/; domain=.yijincc.com; secure; HttpOnly',
        'QCWx_0e98_loginuser=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; domain=.yijincc.com; secure',
        'QCWx_0e98_activationauth=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; domain=.yijincc.com; secure',
        'QCWx_0e98_pmnum=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; domain=.yijincc.com; secure',
        'QCWx_0e98_lastcheckfeed=3652%7C1595573103; expires=Sat, 24-Jul-2021 06:45:03 GMT; Max-Age=31536000; path=/; domain=.yijincc.com; secure',
        'QCWx_0e98_checkfollow=1; expires=Fri, 24-Jul-2020 06:45:33 GMT; Max-Age=30; path=/; domain=.yijincc.com; secure',
        'QCWx_0e98_lip=110.19.61.151%2C1595573057; path=/; domain=.yijincc.com; secure',
    ]
    def __init__(self,username,password):
        self.data = {
            'formhash': 'c843f964',
            'referer': 'https://ke.yijincc.com/school.htm',
            'phone_login': username,
            'phone_password': password,
            'cookietime': '2592000',
        }

    def transfer_cookie_dict(self, cookie_str):
        item = {}
        coo = cookie_str.split('; ')
        for i in coo:
            li = i.split('=')
            item[li[0]] = li[1]
        return item

    def request_url(self,url):
        self.session = requests.Session()
        res = self.session.post(url=url, headers=self.headers, data=self.data)
        coo = RequestsCookieJar()
        for name, value in res.cookies.items():
            coo.set(name, value, path='/', domain='.yijincc.com; secure')
            self.session.cookies.update(coo)

    def now_timestamp(self,shu=1):
        return round(datetime.now().timestamp()*shu)
    def chrome_get_key(self,url):
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--window-size=1366,768')
        # chrome_options.add_experimental_option('prefs', prefs) #禁止浏览器弹窗
        # chrome_options.add_experimental_option('useAutomationExtension', False)
        # chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])  # 去掉正在被自动化检测程序控制的提示
        chrome_options.add_argument('--headless')
        self.driver = webdriver.Chrome(executable_path='chromedriver1.exe', chrome_options=chrome_options)
        self.driver.get(url)
        time.sleep(1)
        page = self.driver.page_source
        self.driver.quit()
        return page
    def take_target_file_m3u8(self,url):
        resp = requests.get(url=url,headers=self.headers1).text
        url_m3u8 = self.is_empty(self.my_xpath(resp,'//*[@id="v"]/@value'))
        result = requests.get(url=url_m3u8, headers=self.headers).text
        host = re.findall(r'(https://aliyun.yijincc.com/.*?/)',url_m3u8)
        if host:
            key_url = re.findall(r'#EXT-X-KEY:METHOD=.*?,URI="(.*?)"\n',result)
            if key_url:
                key = self.chrome_get_key(key_url[0])
                # key = requests.get(url=key_url[0], headers=self.headers).text
                # with open('E:\\a.txt','w',encoding='utf-8') as f:
                #     f.write(key)
                # # video_ts_list = re.findall(r',\n(.*?.ts)', result)
                # video_ts_urls = [host[0]+i for i in re.findall(r',\n(.*?.ts.*?)\n', result)]
                # print(video_ts_urls)
                print(key)

    def run(self):
        # url = 'https://ke.yijincc.com/plugin.php?id=jzsjiale_sms:homesms&act=phonemimalogin&loginsubmit=yes&inajax=1'
        # self.request_url(url)
        self.take_target_file_m3u8('https://ke.yijincc.com/play?kid=109')
if __name__ == '__main__':
    jy = YinYI(USER,PASSWORD)
    jy.run()
